import React, { useEffect, useState } from 'react';
import ReactECharts from 'echarts-for-react';
import * as echarts from 'echarts';
import { useWindowSize } from '../hooks/useWindowSize';

const BarChart: React.FC = () => {
  const { width } = useWindowSize();
  const [fontSize, setFontSize] = useState(16);
  const [chartHeight, setChartHeight] = useState('100%');

  useEffect(() => {
    if (width <= 480) {
      setFontSize(12);
      setChartHeight('200px');
    } else if (width <= 768) {
      setFontSize(14);
      setChartHeight('250px');
    } else if (width <= 992) {
      setFontSize(15);
      setChartHeight('300px');
    } else {
      setFontSize(16);
      setChartHeight('100%');
    }
  }, [width]);

  const option = {
    title: {
      text: '数据统计',
      textStyle: {
        color: '#fff',
        fontSize: fontSize
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      confine: true
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
      top: fontSize + 20
    },
    xAxis: {
      type: 'category',
      data: ['类别1', '类别2', '类别3', '类别4', '类别5', '类别6'],
      axisLine: {
        lineStyle: {
          color: '#fff'
        }
      },
      axisLabel: {
        fontSize: fontSize - 2,
        color: '#fff',
        interval: width <= 480 ? 1 : 0 // 在小屏幕上间隔显示标签
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#fff'
        }
      },
      axisLabel: {
        fontSize: fontSize - 2,
        color: '#fff'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(255, 255, 255, 0.1)'
        }
      }
    },
    series: [
      {
        name: '数值',
        type: 'bar',
        data: [320, 332, 301, 334, 390, 330],
        barWidth: width <= 480 ? '40%' : '60%', // 在小屏幕上减小柱子宽度
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#83bff6' },
            { offset: 0.5, color: '#188df0' },
            { offset: 1, color: '#188df0' }
          ])
        },
        emphasis: {
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#2378f7' },
              { offset: 0.7, color: '#2378f7' },
              { offset: 1, color: '#83bff6' }
            ])
          }
        }
      }
    ]
  };

  return (
    <ReactECharts
      option={option}
      style={{ height: chartHeight, width: '100%' }}
      opts={{ renderer: 'svg' }}
      notMerge={true}
      lazyUpdate={true}
    />
  );
};

export default BarChart; 